const app = getApp(); //定义常量
const util = require('../../../utils/util.js');
const qrCode = require('../../../libs/weapp-qrcode.js');
// var $ = require('../../classHomePage/pk/index/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    course_name: '图形学',
    invite_code: null,
    banji_info: null,
    funList: {
      'funitems': [{
          "name": "PK",
          "url": "/pages/classHomePage/pk/index/index",
          "pic_url": "/assets/wx_pic/menu_pk@3x.png"
        },
        {
          "name": "盲盒",
          "url": "/pages/classHomePage/blindbox/index/index",
          "pic_url": "/assets/wx_pic/menu_mh@3x.png"
        },
        {
          "name": "签到",
          "url": "/pages/classHomePage/signIn/index/index",
          "pic_url": "/assets/wx_pic/menu_qd@3x.png"
        },
        {
          "name": "辩论会",
          "url": "/pages/classHomePage/debate/index/index",
          "pic_url": "/assets/wx_pic/menu_blh@3x.png"
        },
        {
          "name": "抢答",
          "url": "/pages/classHomePage/qiangda/index/index",
          "pic_url": "/assets/wx_pic/menu_qd_1@3x.png"
        },
        {
          "name": "讨论",
          "url": "/pages/classHomePage/discuss/index/index",
          "pic_url": "/assets/wx_pic/menu_tl@3x.png"
        },
        {
          "name": "成员分组",
          "url": "/pages/classHomePage/group/index/index",
          "pic_url": "/assets/wx_pic/menu_zyfz@3x.png"
        },
        {
          "name": "积分",
          "url": "/pages/classHomePage/integral/index/index",
          "pic_url": "/assets/wx_pic/menu_jf@3x.png"
        },
      ]
    },
    click_title: 1,
    show: false, //android上conver-view盖不住canvas的bug,可以做平台判断
    couponNum: 4,
    couponList: [{
      code: null,
      spread: true,
    }],
    qrcode_w: 130,
    people: null,
  },

  click_item: function (e) {
    console.log('click:', e);
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  click_title_box: function (e) {
    this.setData({
      click_title: e.target.dataset['id']
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    this.setData({
      banji_info: app.globalData.banji_info,
      people: wx.getStorageSync('people'),
    })
    if (wx.getStorageSync('people') == 'teacher') {
      _this.data.couponList[0]['code'] = app.globalData.banji_info['classInviteCode']
    }

    wx.getDeviceInfo({
      success: (res) => {
        const system = res.system.toLowerCase(); // 将系统信息转为小写
        if (system.indexOf('ios') > -1) { // 判断是否为 iOS 系统
          this.setData({
            show: true
          });
        }
      }
    });
    const W = wx.getWindowInfo().windowWidth;
    const rate = 750.0 / W;
    //利用比例将260rpx转换为px
    const qrcode_w = 260 / rate;
    this.setData({
      qrcode_w: qrcode_w
    }, () => {
      this.couponQrCode(this.data.couponList[0].code, "couponQrcode0")
    });
  },

  // 二维码生成工具
  couponQrCode(text, canvasId) {
    new qrCode(canvasId, {
      text: text,
      width: this.data.qrcode_w,
      height: this.data.qrcode_w,
      colorDark: "#333333",
      colorLight: "#FFFFFF",
      correctLevel: qrCode.CorrectLevel.H
    });
    if (canvasId == "couponQrcode0") {
      setTimeout(() => {
        if (!this.data.show) {
          this.setData({
            show: true
          })
        }
      }, 0)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})